import 'package:get/get.dart';
import 'package:ty_smart_watch/bean/body_data_bean.dart';
import 'package:ty_smart_watch/bean/sport_recorder_bean.dart';
import 'package:ty_smart_watch/common/function_id.dart';
import 'package:ty_smart_watch/common/images_url.dart';
import 'package:ty_smart_watch/utils/Log.dart';
import 'package:ty_smart_watch/utils/data_util.dart';

class BodyGridController extends GetxController {
  var list = <BodyDataBean>[
    BodyDataBean(HEART, GRID_ICON_HEART, "心率", "0", "bmp", 0xFFFFEEF1),
    BodyDataBean(SPORT_RECORDER, GRID_ICON_SPORT, "运动记录", "0", "公里", 0xFFFFF2E4),
    BodyDataBean(BODY_TEMPERATURE, GRID_ICON_TEMPERATURE, "体温", "0", "℃", 0xFFEBF8FB),
    BodyDataBean(SLEEP, GRID_ICON_SLEEP, "睡眠记录", "0", "", 0xFFF8EFFF),
    BodyDataBean(BLOOD_OXYGEN, GRID_ICON_BLOOD_OXYGEN, "血氧", "0", "%", 0xFFFFE7E4),
    BodyDataBean(BLOOD_PRESSURE, GRID_ICON_BLOOD_PRESSURE, "血压", "0", "", 0xFFFFE7F1),
    BodyDataBean(BLOOD_GLUCOSE, GRID_ICON_BLOOD_GLUCOSE, "血糖", "0", "mmol/L", 0xFFFFEDE9),
    BodyDataBean(ECG, GRID_ICON_ECG, "心电图", "0", "", 0xFFFFFEDB),
    BodyDataBean(RRI, GRID_ICON_RRI, "情绪", "0", "", 0xFFFFFEDB),
    BodyDataBean(HRV, GRID_ICON_HRV, "HRV", "0", "", 0xFFFFFEDB),
  ].obs;

  updateData(int id, String data) {
    // Log.d("更新请求：$id >  $data");
    var item = list.firstWhere((element) => element.id == id);
    item.data = data;
    list.refresh();
  }

  final DateUtil _dateUtil = DateUtil();

  ///更新智能生活页运动记录数据
  updateTodaySportDistance(List<SportBean> sportList) {
    List<SportBean> sportDataList =
        sportList.where((sport) => _dateUtil.isToDay(sport.timeStamp, isMs: true)).toList();
    int mDistance = 0;
    for (var data in sportDataList) {
      if (data.distance != -1) {
        mDistance += data.distance;
      }
    }
    Log.d("今日运动距离总和： $mDistance");
    //更新运动距离总和数据
    var item = list.firstWhere((element) => element.id == SPORT_RECORDER);
    item.data = (mDistance/1000).toStringAsFixed(2);
  }
}
